<!--
 * @Author: 祝肖 1738052358@qq.com
 * @Date: 2024-03-27 14:00:44
 * @LastEditTime: 2024-06-28 16:13:40
 * @LastEditors: 祝肖 1738052358@qq.com
 * @Description: 信用等级评定编辑
 * @FilePath: \zmglpt-ui-mobile-v1.1\dev\pages\query\creditLvAssess\components\levelAssessEdit.vue
 * 可以输入预定的版权声明、个性签名、空行等
-->
<template>
  <view>
    <view class="retail">
      <!-- <fsection title="零售户信息" class="pl-16"></fsection> -->
      <theme-navbar :is-white="false" :title="navTitle" :custom-back="goBack" style="z-index: 99999"></theme-navbar>
      <u-gap :height="$theme.cm_section_top_gap_height" :bg-color="$theme.cm_gap_color"></u-gap>
    </view>
    <view class="rdcontent">
      <u-form :model="farform" :rules="rules" ref="uForm">
        <u-row gutter="16" align="top" class="divided-line">
          <u-col span="6">
            <view class="msg-title">许可证号</view>
            <view class="msg-context">{{ farform.licNo || "/" }}</view>
          </u-col>
          <u-col span="6">
            <view class="msg-title">企业（字号）名称</view>
            <view class="msg-context">{{ farform.companyName || "/" }}</view>
          </u-col>
        </u-row>
        <u-row gutter="16" align="top" class="divided-line">
          <u-col span="6">
            <view class="msg-title">统一社会信用代码</view>
            <view class="msg-context">{{ farform.managerName || "/" }}</view>
          </u-col>
          <u-col span="6">
            <view class="msg-title">负责人</view>
            <view class="msg-context">{{ farform.managerName || "/" }}</view>
          </u-col>
        </u-row>
        <u-row gutter="16" justify="space-between" align="top" class="divided-line">
          <u-col span="6">
            <view class="msg-title">经营地址</view>
            <view class="msg-context">{{ farform.businessAddr || "/" }}</view>
          </u-col>
          <u-col span="6">
            <view class="msg-title">联系电话</view>
            <view class="msg-context">{{ farform.retailTel || "/" }}</view>
          </u-col>
        </u-row>
        <u-row gutter="16" justify="space-between" align="top" class="divided-line">
          <u-col span="6">
            <view class="msg-title">信用等级</view>
            <view class="msg-context">{{ farform.creditLevel || "/" }}</view>
          </u-col>
          <u-col span="6">
            <view class="msg-title">信用分值</view>
            <view class="msg-context">{{ farform.creditScore || "/" }}</view>
          </u-col>
        </u-row>

        <u-row gutter="16" justify="space-between" align="top" :required="true" class="divided-line">
          <u-col span="12">
            <u-form-item :label-position="labelPosition" label="评定日期" :required="true" prop="manualDate">
              <u-input placeholder="请选择" :disabled="true" :custom-style="pointerStyle" v-model="farform.manualDate" @click="dateChange('owner')"></u-input>
            </u-form-item>
          </u-col>
        </u-row>
        <u-row gutter="16" align="top">
          <u-col span="12">
            <form-section title="评定情形"></form-section>
            <!-- <u-form-item label="评定情形" :label-position="labelPosition"> -->
            <!-- width:calc(100% - 20px); -->
            <scroll-view :scroll-y="false" :scroll-x="true" :style="{ height: scrollRealHeight, marginTop: '5px', paddingBottom:'10px'}">
              <u-table v-if="show" class="tb-lay" ref="tbRef">
                <u-tr>
                  <u-th width="7%"></u-th>
                  <u-th width="18%">行为编码</u-th>
                  <u-th width="60%">评定内容</u-th>
                  <u-th width="15%">分值</u-th>
                </u-tr>
                <u-tr v-if="currentPageMainItem.length == 0">
                  <u-td width="100%">~ 暂无数据 ~</u-td>
                </u-tr>
                <template v-else>
                  <u-tr v-for="(item,index) in currentPageMainItem" :key="item.standardUuid">
                    <u-td width="7%" style="padding: 7px 0px">
                      <view class="radios">
                        <checkbox :disabled="item.disabled" style="transform:scale(0.8)" :checked="item.checked" @click.native="handlerSelect(item)" />
                        <!-- <u-checkbox-group :wrap="true">
                            <u-checkbox
                              @change="handlerSelect(item)"
                              v-model="item.checked"
                              :key="index"
                              :name="item.standardUuid"
                            ></u-checkbox>
                          </u-checkbox-group>-->
                        <!-- <u-radio @change="handlerSelect(item)" :name="item.radioName"></u-radio> -->
                      </view>
                    </u-td>
                    <u-td width="18%">{{ item.standardUuid }}</u-td>
                    <u-td width="60%">{{ item.standardName }}</u-td>
                    <u-td width="15%">{{item.creditScore }}</u-td>
                  </u-tr>
                </template>
              </u-table>
            </scroll-view>
            <!-- </u-form-item> -->
          </u-col>
        </u-row>
        <!-- 现场图片信息 -->
        <u-row gutter="16" justify="space-between" align="top" class="divided-line">
          <u-col span="12">
            <u-form-item ref="liveFileRef" :label-position="labelPosition" label="现场图片信息" :required="isRequired" prop="liveFile">
              <view class="zk-img-content">
                <m-upload :deletable="true" :maxCount="picIds" width="150rpx" height="150rpx" :file-list="liveFileList" :multiple="false" index="liveFile"
                  :uploadParam="{'fiGroupCode':$FILE_GROUP_TYPE.m_check_attr, }" upApiUrl="uploadBase64ForApp" ref="filePicUpload" @on-list-change="changeUpload"></m-upload>
              </view>
              <!-- <signature :mustHor="true" :visible="isShowSignView"  @close="isShowSignView = false" @save="saveSign"> -->
              <!-- </signature> -->
            </u-form-item>
          </u-col>
        </u-row>
        <!-- 评定证明图片信息 -->
        <u-row gutter="16" justify="space-between" align="top" class="divided-line">
          <u-col span="12">
            <u-form-item ref="assessDescFileRef" :label-position="labelPosition" label="评定证明图片信息" :required="isRequired" prop="assessDescFile">
              <view class="zk-img-content">
                <m-upload :deletable="true" :maxCount="picIds" width="150rpx" height="150rpx" :file-list="assessDescFileList" :multiple="false" index="assessDescFile"
                  :uploadParam="{'fiGroupCode':$FILE_GROUP_TYPE.m_check_attr, }" upApiUrl="uploadBase64ForApp" ref="filePicUpload" @on-list-change="changeUpload"></m-upload>
              </view>
              <!-- <signature :mustHor="true" :visible="isShowSignView"  @close="isShowSignView = false" @save="saveSign"> -->
              <!-- </signature> -->
            </u-form-item>
          </u-col>
        </u-row>

        <u-row gutter="16" justify="space-between" align="top" class="divided-line">
          <u-col span="12">
            <u-form-item :label-position="labelPosition" :required="true" label="评定说明" prop="remark">
              <u-input type="textarea" height="100" :auto-height="true" placeholder="请输入" v-model="farform.remark" />
            </u-form-item>
          </u-col>
        </u-row>
      </u-form>
      <view class="popup-submit-btns">
        <u-button class="u-btn" type="primary" @click="submitLngLat('01')">保存</u-button>
        <u-button class="u-btn" type="primary" @click="submitLngLat('02')">提交</u-button>
      </view>
    </view>
    <!-- 时间 -->
    <u-picker mode="time" v-model="dateRange" @confirm="handlerSearchDate" @cancel="showTime = false"></u-picker>
  </view>
</template>

<script>
import formSection from "@/components/form-section.vue";
import mUpload from "@/components/m-upload.vue";
import fsection from "@/components/form-section.vue";
import SingleSelect from "@/components/single-select.vue";
import checkService from "@/service/check/check.service.js";
import certificateService from "@/service/certificate/certificate.service.js";
import intelligenceservice from "@/service/intelligence/intelligence.service";

import zjCommonService from "@/service/common/common.service";
// import Signature from "@/components/signature";
export default {
  components: {
    fsection,
    mUpload,
    SingleSelect,
    formSection
  },
  // 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
  onReady() {
    this.$refs.uForm.setRules(this.rules);
  },
  onLoad(opt) {
    console.log(opt.targetUuid, "uuiddddd");
    this.retailerUuid = opt.retailerUuid;
    // this.farform.retailerUuid = opt.retailerUuid;
    this.targetUuid = opt.targetUuid;
    this.maxValue = opt.maxValue;
    console.log("最大分值限额为", this.maxValue);
  },
  data() {
    return {
      scrollRealHeight: "50px",
      show: true,
      maxValue: 0,
      title: {
        farform: "零售户信息"
        // retailerUuid: this.retailerUuid
      },
      currentPageMainItem: [],
      border: false,
      marketGis: undefined,
      retailerUuid: "",
      targetUuid: "",
      isShowSignView: false,
      farform: {
        // retailerUuid:this.retailerUuid,
        creditInspireMainItem: [],
        manualDate: this.$u.date(Date.now(),'yyyy-mm-dd'),//默认当天
        remark: ""
      },
      formData: {},
      specialTypeName: "",
      retailId: "",
      lnglat: null,
      liveFileList: [], // 现场文件列表
      assessDescFileList: [], // 评定证明图片列表
      dictList: [],
      // rules: [],
      // h5端部分浏览器input设置disabled后点击事件失效 2023-10-27 fsx start
      pointerStyle: {
        marginRight: "5px",
        pointerEvents: "none !important"
      },
      picIds: 3,
      isRequired:true,
      labelPosition: "top",
      dateRange: false,
      // 模式
      //   paramsTime: {
      //     year: true,
      //     month: true,
      //     day: true,
      //     hour: true,
      //     minute: true,
      //     second: true
      //   },
      rules: {
        liveFile: [
          {
            validator: (rule, value, callback) => {
              if (this.liveFileList.length) {
                return true;
              } else {
                return false;
              }
            },
            message: "请上传现场图片",
            trigger: ["change", "blur"]
          }
        ],
        assessDescFile: [
          {
            validator: (rule, value, callback) => {
              if (this.assessDescFileList.length) {
                return true;
              } else {
                return false;
              }
            },
            message: "请上传评定证明图片",
            trigger: ["change", "blur"]
          }
        ],
        remark: [
          {
            required: true,
            message: "请输入评定说明",
            trigger: ["change", "blur"]
          }
        ],
        manualDate: [
          {
            required: true,
            message: "请选择评定日期！",
            trigger: ["change", "blur"]
          }
        ],
        sender: [
          {
            message: "发件人格式不正确！",
            trigger: ["change", "blur"],
            validator: (rule, value, callback) => {
              if (this.$u.test.isEmpty(value)) {
                return true;
              }
              {
                let regs = /^[a-zA-Z\u4e00-\u9fa5]+$/;
                return regs.test(value);
              }
            }
          }
        ]
      },
      rules1:{
        //  liveFile: [
        //   {
        //     validator: (rule, value, callback) => {
        //       if (this.liveFileList.length) {
        //         return true;
        //       } else {
        //         return false;
        //       }
        //     },
        //     message: "请上传现场图片",
        //     trigger: ["change", "blur"]
        //   }
        // ],
        // assessDescFile: [
        //   {
        //     validator: (rule, value, callback) => {
        //       if (this.assessDescFileList.length) {
        //         return true;
        //       } else {
        //         return false;
        //       }
        //     },
        //     message: "请上传评定证明图片",
        //     trigger: ["change", "blur"]
        //   }
        // ],
        remark: [
          {
            required: true,
            message: "请输入评定说明",
            trigger: ["change", "blur"]
          }
        ],
        manualDate: [
          {
            required: true,
            message: "请选择评定日期！",
            trigger: ["change", "blur"]
          }
        ],
        sender: [
          {
            message: "发件人格式不正确！",
            trigger: ["change", "blur"],
            validator: (rule, value, callback) => {
              if (this.$u.test.isEmpty(value)) {
                return true;
              }
              {
                let regs = /^[a-zA-Z\u4e00-\u9fa5]+$/;
                return regs.test(value);
              }
            }
          }
        ]
      }
    };
  },
  created() {
    // this.marketGis={}
  },
  mounted() {
    this.getData();
  },
  methods: {
    getData() {
      checkService
        .getInspireDetail({
          retailerUuid: this.retailerUuid,
          targetUuid: this.targetUuid
        }) //getInspireDetail
        .then(res => {
          if (res.success) {
            this.farform = res.bean;
            if(!this.farform.manualDate){
              this.farform.manualDate=this.$u.date(Date.now(),'yyyy-mm-dd')//默认当天
            }
            this.currentPageMainItem = this.farform.creditInspireMainItem;
            this.farform.creditInspireMainItem = [];
            if (this.currentPageMainItem.length) {
              const _hasInspireItemUuid = this.currentPageMainItem.some(
                s => s.inspireItemUuid
              );
              this.currentPageMainItem.forEach(element => {
                if (_hasInspireItemUuid) {
                  if (element.inspireItemUuid) {
                    element.checked = true;
                    element.disabled = false;
                  } else {
                    element.checked = false;
                    element.disabled = element.isRequired === "1";
                  }
                } else {
                  element.checked = false;
                  element.disabled = false;
                }
              });
              setTimeout(() => {
                this.scrollRealHeight =
                  this.$refs.tbRef.$el.clientHeight + 5 + "px";
              }, 100);
            }
            console.log(
              res.bean.inspireMainUuid,
              "此处打印了业务id，用来调用图片接口"
            );
            let params = {
              bizId: res.bean.inspireMainUuid,
              group: this.$FILE_GROUP_TYPE.m_check_attr,
              menuCode: null
            };
            intelligenceservice.findFilesByBizIdAndGroup(params).then(res => {
              if (res.success) {
                // 对请求回的附件分组
                this.liveFileList = [];
                this.assessDescFileList = [];
                res.data?.forEach(list => {
                  if (list.gfiStoreName.includes("assessDesc_")) {
                    this.assessDescFileList.push(list); // 现场文件列表
                  } else {
                    this.liveFileList.push(list); // 评定证明图片列表
                  }
                });
              }
            });
            console.log(this.$FILE_GROUP_TYPE, "this.$FILE_GROUP_TYPE");
          }
        });
    },
    getListViewHeight() {
      let p =
        this.$u.sys().windowHeight -
        44 -
        uni.upx2px(180) -
        this.$u.sys().statusBarHeight +
        "px";
      return p;
    },
    groupByFileName() {},
    handlerSelect(item) {
      this.currentPageMainItem.forEach((element, index) => {
        if (item.isRequired) {//用isRequired判断单选多选
          if (item.standardUuid == element.standardUuid) {
            element.checked = !element.checked;
            element.disabled = false;
          } else {
            
            if (item.targetUuid == "HL10000007") {//交易数据-数据对接客户区分为两组做单选
              if (element.targetUuid == "HL10000007") {
                element.checked = false;
                element.disabled = true;
              } else{
                // element.checked = false;
                if(element.checked){
                  element.disabled = false;
                }
              }
            } else if (item.targetUuid == "HL10000015") {//交易数据-数据对接客户区分为两组做单选
              if (element.targetUuid == "HL10000015") {
                element.checked = false;
                element.disabled = true;
              } else{
                if(element.checked){
                  element.disabled = false;
                }
              }
            } else {//正常情况直接单选
              element.checked = false;
              element.disabled = true;
            }
          }
        } else {
          if (item.standardUuid == element.standardUuid) {
            element.checked = !element.checked;
            element.disabled = false;
          } else {
            if (element.isRequired) {
              element.checked = false;
              element.disabled = true;
            } else {
              element.disabled = false;
            }
          }
        }
      });
      this.show = !this.show;
      this.$nextTick(() => {
        this.show = !this.show;
      });
      let formCurrent=JSON.parse(JSON.stringify(this.farform))
      // HL1000000302    HL1000000402   HL1000000504   HL1000000604  HL1000000803  
      if(item.standardUuid=="HL1000000201"||item.standardUuid=='HL1000000302'||item.standardUuid=='HL1000000402'||item.standardUuid=='HL1000000504'||item.standardUuid=='HL1000000604'||item.standardUuid=='HL1000000803'){//领导要求"六个月内没有发生无故退货行为"和“无评价记录”的图片非必填
        this.$refs.uForm.resetFields();
        this.farform = formCurrent
        this.isRequired=false
        this.$refs.uForm.setRules(this.rules1);
      }else{
        this.isRequired=true
        this.$refs.uForm.setRules(this.rules);
      }
    },
    dateChange(id) {
      this.dateStatus = id;
      this.dateRange = true;
    },
    submitLngLat(type) {
      this.$refs.uForm.validate(valid => {
        if (!valid) return;
        // if(!this.farform.creditInspireMainItem.length){
        //   this.$u.error("请选择评定情形！");
        //   return;
        // }
        //   this.creditData.fileList = [];
        //   if (this.$refs.creditRepair.areaList.length > 0) {
        //     this.creditData.fileList = Array.from(
        //       new Set(this.$refs.creditRepair.areaList)
        //     );
        //   }
        let num = 0;
        this.currentPageMainItem.forEach(element => {
          if (element.checked) {
            num += Number(element.creditScore);
          }
        });
        console.log(num, "之和");
        console.log(this.maxValue, "最大限值");
        if (num > this.maxValue) {
          this.$u.toast("激励分值之和大于最大限值，请重新选择");
          return;
        }
        this.farform.creditInspireMainItem = this.currentPageMainItem.filter(
          item => item.checked
        );
        if(!this.farform.creditInspireMainItem.length){
            this.$u.error("请选择评定情形！");
          return;
        }
        // 合并 现场图片 和 评定证明图片 附件
        const _fileList = [...this.liveFileList, ...this.assessDescFileList];
        this.farform.fileList = _fileList;
        this.farform.retailerUuid = this.retailerUuid;
        this.farform.inspireStatus = type;
        console.log(this.farform, "this.farform");
        if (type == "01") {
          checkService.saveLicCreditInspire(this.farform).then(res => {
            if (res.success) {
              this.$modal.alert('保存成功', () => {
                this.backPrev({
                  paramKey: 'refreshBack',
                  param: true,
                });
              });
              // this.$u.success("保存成功！");
              // this.addStimulateManagePage = false;
              // this.refreshTable();
            } else {
              this.$u.error(res.msg);
            }
          });
        } else {
          checkService.saveLicCreditInspire(this.farform).then(res => {
            if (res.success) {
              // this.$u.success("提交成功！");
              this.$modal.alert('提交成功', () => {
                this.backPrev({
                  paramKey: 'refreshBack',
                  param: true,
                });
              });
            } else {
              this.$u.error(res.msg);
            }
          });
        }
      });
    },
    updateBackState(params) {},
    // 上传/删除图片事件
    changeUpload(list, key) {
      console.log(list, key, "upppppp");
      if (list.length > 0) {
        // 设置文件名前缀区分图片分组 现场图片（''） 评定证明图片（assessDesc_）
        // 此处现场图片保留了之前的原图片 故不进行文件名修改
        const _prefix = key === "liveFile" ? "" : "assessDesc_";
        let _storList = list.map(v => {
          if (!v.gfiStoreName.includes(_prefix)) {
            v.gfiStoreName = _prefix + v.gfiStoreName;
          }
          return v;
        });
        this[`${key}` + "List"] = [..._storList];
      } else {
        this[`${key}` + "List"] = [];
      }
      console.log(this[`${key}` + "List"], "listttttt");
      this.$refs[`${key + "Ref"}`].resetField();
    },
    choose(data) {
      if (data) {
        this.dictShow = false;
      }
    },
    handlerSearchDate(e) {
      let time = e.year + "-" + e.month + "-" + e.day + " ";
      this.farform.manualDate = time;
    }
  }
};
</script>

<style lang="scss" scoped>
@import "../../../retailer/retailerInfo/retailerItem/retailItem.scss";

.img-size {
  width: 32rpx;
  height: 42rpx;
  vertical-align: middle;
}
// .radios {
//   display: flex;
//   align-items: center;
//   justify-content: center;
//   width: 100%;
//   height: 61.5rpx;
// }
.lastTd {
  // overflow: hidden;
  // text-overflow: ellipsis;
  // display: -webkit-box;
  // // -webkit-line-clamp: 2;
  // -webkit-box-orient: vertical;
  // -moz-box-orient:vertical;
  /* 设置宽度和高度 */
  // width: 398rpx;
  // height: 89rpx;
  // white-space: nowrap; //禁止换行
  // overflow: hidden;
  // text-overflow: ellipsis; //...
  // overflow: hidden; /* 确保超出容器的文本被裁剪 */
  // // white-space: nowrap; /* 确保文本在一行内显示 */
  // text-overflow: ellipsis; /* 使用省略号表示文本超出 */
}
.rdcontent {
  padding-bottom: 100px;
}
.popup-submit-btns {
  width: calc(100% - 30px);
  position: fixed;
  bottom: 10rpx;
}
.tb-lay {
  width: 900px;
  margin-left: 1px;
  margin-right: 1px;

  // overflow-x: scroll;
  // width: calc(100%-1px);
}
.td-text {
  word-break: break-all;
  word-wrap: break-word;
}
/deep/.u-td {
  height: auto;
}
</style>
